package com.jspxcms.plug.web.fore;

import com.jspxcms.common.util.JsonResponse;
import com.jspxcms.core.domain.Site;
import com.jspxcms.core.domain.User;
import com.jspxcms.core.domain.UserDetail;
import com.jspxcms.core.service.ScoreService;
import com.jspxcms.core.service.UserDetailService;
import com.jspxcms.core.support.Context;
import com.jspxcms.core.support.ForeContext;
import com.jspxcms.plug.domain.UserFavorite;
import com.jspxcms.plug.service.UserFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

/**
 * Created by yuanyue on 14-9-21.
 */
@Controller
public class UserFavoriteController {

    @Autowired
    private UserDetailService userDetailService;

    @RequestMapping(value = "/user_favorite/save.jspx", produces = "text/plain;charset=UTF-8")
    public Object saveFavorite(
            Integer id,
            String title,
            Integer nodeid,
            Integer type,
            HttpServletRequest request,
            HttpServletResponse response,
            org.springframework.ui.Model modelMap) {
        Site site = Context.getCurrentSite(request);
        Map<String, Object> data = modelMap.asMap();
        ForeContext.setData(data, request);
        User user = Context.getCurrentUser(request);
        if (user != null) {
            UserFavorite favorite = new UserFavorite();
            favorite.setCreationDate(new Date());
            favorite.setNewsId(id);
            favorite.setNewsTitle(title);
            favorite.setNewsTypeId(nodeid);
            favorite.setTypeId(type);
            favorite.setUserId(user.getId());
            if (!userFavoriteService.exists(user.getId(), id)) {
                userFavoriteService.save(favorite);
                if (type == 1) {
                    UserDetail detail = userDetailService.get(user.getId());
                    detail = scoreService.updateScore(detail, "score_favorite");
                    userDetailService.update(detail);
                } else if (type == 2) {
                    UserDetail detail = userDetailService.get(user.getId());
                    scoreService.updateScore(detail, "score_study_online");
                    userDetailService.update(detail);
                }
                return JsonResponse.jsonOutput(response, 1, "收藏成功");
            } else {
                return JsonResponse.jsonOutput(response, -1, "您已经收藏了此文章");
            }
        } else {
            return JsonResponse.jsonOutput(response, -2, "您未登录请先登录");
        }
    }

    @RequestMapping(value = "/user_favorite/delete.jspx", produces = "text/plain;charset=UTF-8")
    public Object deleteFavorite(
            Integer newsid,
            HttpServletRequest request,
            HttpServletResponse response,
            org.springframework.ui.Model modelMap) {
        Site site = Context.getCurrentSite(request);
        Map<String, Object> data = modelMap.asMap();
        ForeContext.setData(data, request);
        User user = Context.getCurrentUser(request);
        if (user != null) {
            if (userFavoriteService.delete(user.getId(), newsid)) {
                return JsonResponse.jsonOutput(response, 1, "删除成功");
            } else {
                return JsonResponse.jsonOutput(response, -1, "删除失败");
            }
        } else {
            return JsonResponse.jsonOutput(response, -2, "您未登录请先登录");
        }
    }

    @Autowired
    private UserFavoriteService userFavoriteService;

    @Autowired
    ScoreService scoreService;
}
